home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCommon / Sources / FWPriMem.cpp next >
Encoding:
Text File  |  1995-11-08  |  3.8 KB  |  157 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPriMem.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   FWPRIMEM_H
  13. #include "FWPriMem.h"
  14. #endif
  15.  
  16. // ----- OpenDoc Includes -----
  17.  
  18. #define FW_OPENDOC 1
  19.  
  20. #if FW_OPENDOC && defined(FW_BUILD_MAC)
  21. #include <MemMgr.h>
  22. #endif
  23.  
  24. #if FW_OPENDOC && defined(FW_BUILD_WIN)
  25. #include <MMStubs.h>
  26. #endif
  27.  
  28. // ----- Platform Includes -----
  29.  
  30. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWS)
  31. #include <Windows.h>
  32. #endif
  33.  
  34.  
  35. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWSX)
  36. #include <WindowsX.h>
  37. #endif
  38.  
  39. #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
  40. #include <Memory.h>
  41. #endif
  42.  
  43. #if FW_LIB_EXPORT_PRAGMAS
  44. #pragma lib_export on
  45. #endif
  46.  
  47. #ifdef FW_BUILD_MAC
  48. #pragma segment FWCommon
  49. #endif
  50.  
  51. //----------------------------------------------------------------------------------------
  52. // FW_PrimitiveAllocateBlock
  53. //----------------------------------------------------------------------------------------
  54. FW_FUNC_ATTR void* FW_PrimitiveAllocateBlock(size_t n)
  55. {
  56. #if FW_OPENDOC
  57.     return MMAllocate(n);
  58. #elif defined(FW_BUILD_MAC)
  59.     return NewPtr(n);
  60. #elif defined(FW_BUILD_WIN)
  61.     return GlobalAllocPtr(GHND, n);
  62. #endif
  63. }
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // FW_PrimitiveResizeBlock
  67. //----------------------------------------------------------------------------------------
  68. FW_FUNC_ATTR void* FW_PrimitiveResizeBlock(void* p,
  69.                                            size_t n)
  70. {
  71. #if FW_OPENDOC
  72.     return MMReallocate(p, n);
  73. #elif defined(FW_BUILD_MAC)
  74.     void* q = p;
  75.     if (q==NULL)
  76.         q = FW_PrimitiveAllocateBlock(n);
  77.     else
  78.     {
  79.         SetPtrSize((Ptr) q, n);
  80.         if (MemError() != noErr)
  81.         {
  82.             q = FW_PrimitiveAllocateBlock(n);
  83.             if (q)
  84.             {
  85.                 BlockMoveData(p, q, n);
  86.                 FW_PrimitiveFreeBlock(p);
  87.             }
  88.         }
  89.     }
  90.     return q;
  91. #elif defined(FW_BUILD_WIN)
  92.     return GlobalReAllocPtr(p, n, 0);
  93. #endif
  94. }
  95.  
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // FW_PrimitiveGetBlockSize
  99. //----------------------------------------------------------------------------------------
  100. size_t FW_FUNC_ATTR FW_PrimitiveGetBlockSize(void* p)
  101. {
  102. #if FW_OPENDOC
  103.     return MMBlockSize(p);
  104. #elif defined(FW_BUILD_MAC)
  105.     return GetPtrSize((Ptr) p);
  106. #elif defined(FW_BUILD_WIN)
  107.     return GlobalSize(GlobalPtrHandle(p));
  108. #endif
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // FW_PrimitiveFreeBlock
  113. //----------------------------------------------------------------------------------------
  114. void FW_FUNC_ATTR FW_PrimitiveFreeBlock(void* p)
  115. {
  116. #if FW_OPENDOC
  117.     if (p)
  118.         MMFree(p);
  119. #elif defined(FW_BUILD_MAC)
  120.     if (p)
  121.         DisposePtr((Ptr) p);
  122. #elif defined(FW_BUILD_WIN)
  123.     if (p)
  124.         GlobalFreePtr(p);
  125. #endif
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // FW_PrimitiveCopyMemory
  130. //----------------------------------------------------------------------------------------
  131.  
  132. void FW_FUNC_ATTR FW_PrimitiveCopyMemory(const void *source, void *destination, size_t bytes)
  133. {
  134. #if defined(FW_BUILD_MAC)
  135.     ::BlockMoveData(source, destination, bytes);
  136. #elif defined(FW_BUILD_WIN)
  137.     ::MoveMemory(destination, source, bytes);
  138. #else
  139.     // More efficient code is desirable. Don't want to use c standard library.
  140.     // This code must work correctly for overlapping blocks.
  141.     if (source > destination)
  142.     {
  143.         char *dst = (char *) destination;
  144.         const char *src = (const char *) source;
  145.         while (bytes--)
  146.             *dst++ = *src++;
  147.     }
  148.     else if (source < destination)
  149.     {
  150.         char *dst = (char *) destination + bytes;
  151.         const char *src = (const char *) source + bytes;
  152.         while (bytes--)
  153.             *--dst = *--src;
  154.     }
  155. #endif
  156. }
  157.